<template>
  <UploadExcel :onSuccess="onSuccess"></UploadExcel>
</template>

<script setup>
import UploadExcel from '@/components/UploadExcel/index.vue'
import { upLoadManage } from '@/api/user-manage'
import { USER_RELATIONS, formatDate } from './utils'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'

const router = useRouter()
const onSuccess = async (data) => {
  const updata = getManageList(data)
  await upLoadManage(updata)
  ElMessage.success(updata.length + '条数据上传成功')
  router.push('/user/manage')
}
const getManageList = ({ header, results }) => {
  const result = []
  results.forEach((item) => {
    const userObj = {}
    Object.keys(item).forEach((key) => {
      if (key === 'openTime') {
        item[key] = formatDate(item[key])
      }
      userObj[USER_RELATIONS[key]] = item[key]
    })
    result.push(userObj)
  })
  return result
}
</script>

<style lang="scss" scoped></style>
